package two_xia.myPainter;

import two_xia.myPainter.shape.Shape;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Stack;

public class DrawingArea extends JPanel {
    //用于存储绘画记录
    private ArrayList<Shape> shapeArray = new ArrayList<>();
    private Stack<Shape> undoStack = new Stack<>();

    public DrawingArea(ArrayList<Shape> shapeArray, Stack<Shape> undoStack) throws HeadlessException {
        this.shapeArray = shapeArray;
        this.undoStack = undoStack;
    }

    public ArrayList<Shape> getShapeArray() {
        return shapeArray;
    }

    @Override
    public void paint(Graphics g1) {
        Graphics2D g = (Graphics2D) g1;
        System.out.println("repaint");
        super.paint(g);
        // 循环遍历shapeArray数组
        // 从数组中获取Shape对象
        for (Shape shape : shapeArray) {
            if (shape != null)//shape是否不为空，如果不为空则调用画图形的方法
                shape.draw(g);//调用绘制图形的方法
        }
    }
}
